package com.zhu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.domain.Goods;
import com.zhu.mapper.GoodsMapper;
import com.zhu.model.goods.GoodsConditionSelectVM;
import com.zhu.model.goods.GoodsInsertVM;
import com.zhu.service.GoodsService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author JoyBoy
 * @since 2022-06-02
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Override
    public boolean saveGoods(Goods goods) {
        return this.save(goods);
    }

    @Override
    public boolean deleteGoods(Integer goodsId) {
        return this.removeById(goodsId);
    }

    @Override
    public Goods getGoodsById(Integer goodsId) {
        return this.getById(goodsId);
    }

    @Override
    public Goods getGoodsByName(String goodsName) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_name", goodsName);
        return this.getOne(queryWrapper);
    }

    @Override
    public Goods getGoodsByCondition(GoodsInsertVM model) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_name", model.getGoodsName());
        queryWrapper.eq("color", model.getGoodsName());
        queryWrapper.eq("size", model.getGoodsName());
        return this.getOne(queryWrapper);
    }

    @Override
    public List<Goods> getGoodsList(GoodsConditionSelectVM model) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        if (model.getGid() == null && model.getGoodsName() == null && model.getSize() == null && model.getMinPrice() == null && model.getMaxPrice() == null) {
            return this.list();
        } else {
            if (model.getGid() != null) {
                queryWrapper.like("gid", model.getGid());
            }

            if (model.getGoodsName() != null) {
                queryWrapper.like("goods_name", model.getGoodsName());
            }

            if (!Objects.equals(model.getSize(), "") && model.getSize() != null) {
                queryWrapper.eq("size", model.getSize());
            }
            if (model.getMinPrice() != null) {
                queryWrapper.ge("price", model.getMinPrice());
            }
            if (model.getMaxPrice() != null) {
                queryWrapper.le("price", model.getMaxPrice());
            }
            return this.list(queryWrapper);
        }
    }

    @Override
    public List<Goods> getAllGoods() {
        return this.list();
    }


    @Override
    public boolean updateGoods(Goods goods) {
        return this.updateById(goods);
    }


}
